package cn.hnu.qq.server.thread;

import java.awt.EventQueue;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class ListenThread extends Thread{

	private  ServerSocket ss;
	
	private static ListenThread liThread;
	
	private Map<String,ClientServerThread> openedThread = new HashMap<String, ClientServerThread>();
	
	private ListenThread(){
		try {
			ss = new ServerSocket(10001);
		} catch (IOException e) {
			System.err.println("服务器启动失败");
			e.printStackTrace();
		}
	}
	
	public static ListenThread getInstance(){
		if(liThread == null){
			liThread = new ListenThread();
		}
		return liThread;
	}

	public void run() {
		while(true){
			try {
				Socket s = ss.accept();
				System.out.println("客户端连接成功:"+s.getInetAddress());
				ClientServerThread cst = new ClientServerThread(s);
				new Thread(cst).start();
			} catch (IOException e) {
				System.err.println("等待连接失败");
				e.printStackTrace();
			}
		}
	}
	
	
	public ClientServerThread getClientServerThread(String ip){
		return openedThread.get(ip);
	}

	public void petClientServerThread(String acount,
			ClientServerThread clientServerThread) {
		openedThread.put(acount, clientServerThread);
		
	}
}
